# How Does Improvement in Commuting Affect Employees? Evidence from a Natural Experiment
# Version: 20211014

## Yao Lu (luyao@sem.tsinghua.edu.cn),        Tsinghua University
## Xinzheng Shi (shixzh@sem.tsinghua.edu.cn), Tsinghua University
## Jagadeesh Sivadasan (jagadees@umich.edu),  University of Michigan
## Zhufeng Xu (zhufeng@zhufengxu.com),        Central University of Finance and Economics

## OS ##########################################################################
OS       <- "macOS"
OS_index <- sapply(OS, FUN = function(pt) any(grepl(pattern = pt, x = sessionInfo())))
if (any(OS_index)) {
  print(paste0("#### Scripts are tested under your OS: ", OS[OS_index], ". ####"))
} else {
  print("#### Warning: Scripts running are not tested under your OS. ####")
}
rm(OS, OS_index)

## Load Packages ################################################################
pkgs      <- c("devtools", "dplyr", "ggfortify", "ggmap", "ggplot2", "haven", 
               "latex2exp", "lfe", "MatchIt", "RItools", "RStata", "survival", "textables")
pkgs_info <- !(pkgs %in% installed.packages())
if (identical(pkgs[pkgs_info], character(length = 0))) {
  lapply(pkgs, require, character.only = TRUE)
} else {
  if ("textables" %in% pkgs[pkgs_info]) {
    pkgs_to_install <- setdiff(pkgs[pkgs_info], "textables")
    lapply(pkgs_to_install, install.packages, character.only = TRUE)
    devtools::install_github("setzler/textables")
  } else {
    lapply(pkgs[pkgs_info], install.packages, character.only = TRUE)
  }
  lapply(pkgs,            require,          character.only = TRUE)
}
rm(pkgs, pkgs_info)

## Load Utilities ##############################################################
source("./Scripts/1Utilities.R")